<?php
declare (strict_types=1);

namespace app\exam\model\admin;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 后台管理
 */
class QuestionTopic extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'exam_question_topic';
    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';
    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';
    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id' => 'integer',
    ];

    /**
     * 定义试题类型
     *
     * @var string[]
     */
    protected $typeOptions = [
        1 => '单选',
        2 => '判断',
        3 => '多选',
        4 => '不定项选择',
        5 => '填空',
        6 => '材料',
        7 => '完形填空',
        8 => '简答',
    ];

    /**
     * 设置知识点
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/7
     *
     * @param $value
     * @return string
     */
    public function setPointAttr($value)
    {
        return $value ? ',' . implode(',', array_unique($value)) . ',' : '';
    }

    /**
     * 转换知识点数据格式
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/4
     *
     * @param $value
     * @return string
     */
    public function getPointAttr($value)
    {
        return explode(',', trim($value, ','));
    }

    /**
     * 获取试题知识点名称
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-16
     * @param  [type] $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function getPointStrAttr($value, $data)
    {
        $title = app(QuestionPoint::class)->whereIn('id', $data['point'])->column('title');
        return count($title) ? implode(';', $title) : "";
    }

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 机构
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 获取试题类型选项
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/23
     *
     * @return array[]
     */
    public function getQuestionTypeOptions()
    {
        $options = [];
        foreach ($this->typeOptions as $value => $title) {
            $options[] = [
                'title' => $title,
                'value' => $value
            ];
        }

        return $options;
    }

    /**
     * 获取客观题试题类型选项数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @return array
     */
    public function getObjectiveQuestionTypeOptions()
    {
        $options = [];
        foreach ($this->typeOptions as $value => $title) {
            if (in_array($value, [1, 2, 3, 4, 7])) {
                $options[] = [
                    'title' => $title,
                    'value' => $value
                ];
            }
        }

        return $options;
    }

    /**
     * 试题类型
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/25
     *
     * @param $value
     * @param $data
     * @return string
     */
    public function getQuestionTypeTextAttr($value, $data)
    {
        return $this->typeOptions[$data['question_type']] ?? '';
    }

    /**
     * 试题难度名称
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/25
     *
     * @return \think\model\relation\BelongsTo
     */
    public function difficultyTitle()
    {
        return $this->belongsTo(QuestionDifficult::class, 'difficulty')->bind(['difficulty_title' => 'title']);
    }

    /**
     * 题库名称
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/25
     *
     * @return \think\model\relation\BelongsTo
     */
    public function questionBankTitle()
    {
        return $this->belongsTo(QuestionBank::class, 'question_bank_id')->bind(['question_bank_title' => 'title']);
    }

    /**
     * 题库名称及分类
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/25
     *
     * @return \think\model\relation\BelongsTo
     */
    public function bankTitleAndCates()
    {
        return $this->belongsTo(QuestionBank::class, 'question_bank_id')->bind(['question_bank_title' => 'title', 'bank_title' => 'title', 'bank_cates' => 'category']);
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     *
     * @return   string
     */
    public static function onBeforeInsert($bank)
    {
        // 机构
        $bank->mhm_id = $bank->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 成功创建之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterInsert($category)
    {
    }

    /**
     * 成功修改之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterUpdate($category)
    {
    }

    /**
     * 成功删除之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterDelete($category)
    {
    }

    /**
     * 关联专项选中
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-16
     * @return [type] [description]
     */
    public function checked()
    {
        return $this->hasOne(ModuleSpecialTopic::class, 'topic_id');
    }
}
